//
//  EMAttention.m
//  duo
//
//  Created by fortsven on 14/12/29.
//  Copyright (c) 2014年 hoperun. All rights reserved.
//

#import "EMAttention.h"
#import <QuartzCore/QuartzCore.h>


#define kAlertWidth 280.0f
#define kAlertHeight 160.0f

@interface EMAttention()
{
    BOOL _leftLeave;
}

@property (nonatomic, strong) UILabel *alertTitleLabel;
@property (nonatomic, strong) UILabel *alertContentLabel;
@property (nonatomic, strong) UIButton *leftBtn;
@property (nonatomic, strong) UIButton *rightBtn;
@property (nonatomic, strong) UIView *backImageView;


@end

@implementation EMAttention

// 提醒框宽度设定方法
+ (CGFloat)alertWidth
{
    return kAlertWidth;
}
// 提醒框高度设定方法
+ (CGFloat)alertHeight
{
    return kAlertHeight;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

#define kTitleYOffset 15.0f
#define kTitleHeight 25.0f

#define kContentOffset 30.0f
#define kBetweenLabelOffset 20.0f
// 自定义提醒框的初始化方法
- (id)initWithTitle:(NSString *)title
        contentText:(NSString *)content
    leftButtonTitle:(NSString *)leftTitle
   rightButtonTitle:(NSString *)rigthTitle
{
    if (self = [super init])
    {
        
        //
        UIImageView *logoLabel = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_logo"]];
        logoLabel.frame = CGRectMake(kAlertWidth / 4, kTitleYOffset, kAlertWidth / 2, kTitleHeight);
        [self addSubview:logoLabel];
        
        // 为消息提醒框UITextField设置圆角
        self.layer.cornerRadius = 15.0;
        // 设置提醒框背景颜色
        self.backgroundColor = RGB(224, 224, 224);
        // 设置提醒框标题的位置
        self.alertTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, kTitleYOffset, kAlertWidth, kTitleHeight)];
        // 设置提醒框标题文字的字体字号
        self.alertTitleLabel.font = [UIFont boldSystemFontOfSize:20.0f];
        // 设置提醒框标题文本的颜色
        self.alertTitleLabel.textColor = [UIColor colorWithRed:56.0/255.0 green:64.0/255.0 blue:71.0/255.0 alpha:1];
        // 将提醒框标题标签添加进来
//        [self addSubview:self.alertTitleLabel];
        // 设置提醒框正文的位置
        CGFloat contentLabelWidth = kAlertWidth - 16;
        self.alertContentLabel = [[UILabel alloc] initWithFrame:CGRectMake((kAlertWidth - contentLabelWidth) * 0.5, CGRectGetMaxY(self.alertTitleLabel.frame), contentLabelWidth, 60)];
        // 设置提醒框正文不限定文本行数
        self.alertContentLabel.numberOfLines = 0;
        // 设置提醒框标题和正文的对齐方式为居中对齐
        self.alertContentLabel.textAlignment = self.alertTitleLabel.textAlignment = NSTextAlignmentLeft;
        // 设置提醒框正文的颜色
        self.alertContentLabel.textColor = [UIColor colorWithRed:127.0/255.0 green:127.0/255.0 blue:127.0/255.0 alpha:1];
        // 设置提醒框正文的字体字号
        self.alertContentLabel.font = [UIFont fontWithName:HEITIMEDIUM size:16];
        // 将提醒框的正文添加进来
        [self addSubview:self.alertContentLabel];
        // 定义提醒框中左右按钮的frame
        CGRect leftBtnFrame;
        CGRect rightBtnFrame;
#define kSingleButtonWidth 160.0f
#define kCoupleButtonWidth 107.0f
#define kButtonHeight 40.0f
#define kButtonBottomOffset 10.0f
        if (!leftTitle)
        {
            // 只有一个按钮时，设置右边按钮的位置和大小和样式
            rightBtnFrame = CGRectMake((kAlertWidth - kSingleButtonWidth) * 0.5, kAlertHeight - kButtonBottomOffset - kButtonHeight, kSingleButtonWidth, kButtonHeight);
            self.rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            self.rightBtn.frame = rightBtnFrame;

        }
        else
        {
            // 有两个按钮时，设置两个按钮的位置、大小和样式
            leftBtnFrame = CGRectMake((kAlertWidth - 2 * kCoupleButtonWidth - kButtonBottomOffset) * 0.5, kAlertHeight - kButtonBottomOffset - kButtonHeight, kCoupleButtonWidth, kButtonHeight);
            rightBtnFrame = CGRectMake(CGRectGetMaxX(leftBtnFrame) + kButtonBottomOffset, kAlertHeight - kButtonBottomOffset - kButtonHeight, kCoupleButtonWidth, kButtonHeight);
            self.leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            self.rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            self.leftBtn.frame = leftBtnFrame;
            self.rightBtn.frame = rightBtnFrame;

        }
        //
        
        // 设置左右按钮的背景
        [self.rightBtn setBackgroundImage:[UIImage imageWithColor:RGB(112, 179, 81)] forState:UIControlStateNormal];
        [self.leftBtn setBackgroundImage:[UIImage imageWithColor:RGB(160, 160, 160)] forState:UIControlStateNormal];
        // 设置左右按钮的正常状态标题
        [self.rightBtn setTitle:rigthTitle forState:UIControlStateNormal];
        [self.leftBtn setTitle:leftTitle forState:UIControlStateNormal];
        // 设置左右按钮的标题字体字号
        self.leftBtn.titleLabel.font = self.rightBtn.titleLabel.font = [UIFont boldSystemFontOfSize:14];
        // 设置左右按钮的标题颜色
        [self.leftBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self.rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        // 设置左右按钮的点击事件
        [self.leftBtn addTarget:self action:@selector(leftBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
        [self.rightBtn addTarget:self action:@selector(rightBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
        // 设置左右按钮蒙板覆盖边界
        self.leftBtn.layer.masksToBounds = self.rightBtn.layer.masksToBounds = YES;
        // 设置左右按钮蒙板圆角半径
        self.leftBtn.layer.cornerRadius = self.rightBtn.layer.cornerRadius = 3.0;
        // 将左右按钮添加进来
        [self addSubview:self.leftBtn];
        [self addSubview:self.rightBtn];
        // 传入提醒框的标题文本和正文文本
        self.alertTitleLabel.text = title;
        self.alertContentLabel.text = content;
    }
    return self;
}

- (void)leftBtnClicked:(id)sender
{
    // 左按钮离开标记
    _leftLeave = YES;
    // 移出提醒
    [self dismissAlert];
    if (self.leftBlock)
    {
        self.leftBlock();
    }
}

- (void)rightBtnClicked:(id)sender
{
    _leftLeave = NO;
    [self dismissAlert];
    if (self.rightBlock)
    {
        self.rightBlock();
    }
}

- (void)show
{
    UIWindow *shareWindow = [UIApplication sharedApplication].keyWindow;
    // 设置提醒的位置和大小
    self.frame = CGRectMake((CGRectGetWidth(shareWindow.bounds) - kAlertWidth) * 0.5, - kAlertHeight - 30, kAlertWidth, kAlertHeight);
    // 将提醒视图添加进来
    [shareWindow addSubview:self];
}

- (void)dismissAlert
{
    // 将提醒移出
    [self removeFromSuperview];
    if (self.dismissBlock)
    {
        self.dismissBlock();
    }
}
// 提醒将要移出视图
- (void)removeFromSuperview
{
    // 提醒移出后，回到以前画面
    [self.backImageView removeFromSuperview];
    UIWindow *shareWindow = [UIApplication sharedApplication].keyWindow;
    // 提醒移出动画
    [UIView animateWithDuration:0.6f    // 设定动画的循环周期
                          delay:0.0     // 设定动画的延时时间
                        options:UIViewAnimationOptionCurveEaseOut
                     animations:^{
                         self.frame = CGRectMake((CGRectGetWidth(shareWindow.bounds) - kAlertWidth) * 0.5, CGRectGetHeight(shareWindow.bounds), kAlertWidth, kAlertHeight);
                     }
                     completion:nil
     ];
}
// 提醒将要移动到视图
- (void)willMoveToSuperview:(UIView *)newSuperview
{
    // 确定有新的父视图
    if (newSuperview == nil)
        return;
    UIWindow *shareWindow = [UIApplication sharedApplication].keyWindow;
    
    if (!self.backImageView)
    {
        self.backImageView = [[UIView alloc] initWithFrame:shareWindow.bounds];
    }
    // 设置提醒框出现之后原来屏幕的背景颜色以及其透明程度
    self.backImageView.backgroundColor = [UIColor blackColor];
    self.backImageView.alpha = 0.6f;
    [shareWindow addSubview:self.backImageView];
    //    self.transform = CGAffineTransformMakeRotation(-M_1_PI / 2);
    // 提醒加入动画
    [UIView animateWithDuration:0.35f
                          delay:0.0 options:UIViewAnimationOptionCurveEaseIn
                     animations:^{
                         self.transform = CGAffineTransformMakeRotation(0);    // 动画停留后的旋转角度，可不写此句
                         self.frame = CGRectMake((CGRectGetWidth(shareWindow.bounds) - kAlertWidth) * 0.5, (CGRectGetHeight(shareWindow.bounds) - kAlertHeight) * 0.5 - 30, kAlertWidth, kAlertHeight);   // 动画停留后的位置
                     }
                     completion:nil];
    [super willMoveToSuperview:newSuperview];
}

@end

@implementation UIImage (colorful)

+ (UIImage *)imageWithColor:(UIColor *)color
{
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}
@end
